/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.thread;

import java.util.concurrent.TimeUnit;

/**
 * <p>Project: javaseapp - T10
 * <p>Powered by webrx On 2021-08-27 11:48:21
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class T10 {
    synchronized void m1() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s线程启动运行中%n", tn);

        System.out.println("有异常，会释放锁");
        int i = 5 / 0;


        try {
            TimeUnit.SECONDS.sleep(15);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.printf("%s线程执行完成，结束了。%n", tn);
    }

    void m2() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s线程启动运行中%n", tn);
        System.out.printf("%s线程执行完成，结束了。%n", tn);
    }

    synchronized void m3() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s线程启动运行中%n", tn);
        try {
            TimeUnit.SECONDS.sleep(15);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.printf("%s线程执行完成，结束了。%n", tn);
    }

    public static void main(String[] args) {
        var t = new T10();

        new Thread(t::m1, "A").start();
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        new Thread(t::m3, "B").start();
    }
}
